package com.ilovetolearn.view
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.TimerEvent;
	import flash.system.Capabilities;
	import flash.utils.Timer;
	import flash.system.Capabilities;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.preloaders.DownloadProgressBar;

	public class BeginStudyDownloadProgressBar extends DownloadProgressBar
	{
		private var loadingEffect:LoadingEffect;
		private var _preloader:Sprite;

		public function BeginStudyDownloadProgressBar()
		{
			super();
			loadingEffect=new LoadingEffect();
			addChild(loadingEffect);
		}

		private function resize(e:Event):void
		{
			loadingEffect.x=(Capabilities.screenResolutionX - loadingEffect.width) / 2;
			loadingEffect.y=(Capabilities.screenResolutionY - loadingEffect.height) / 2;
		}

		override public function set preloader(value:Sprite):void
		{
			_preloader=value

			_preloader.addEventListener(ProgressEvent.PROGRESS, load_progress);
			_preloader.addEventListener(Event.COMPLETE, load_complete);
			_preloader.addEventListener(FlexEvent.INIT_PROGRESS, init_progress);
			_preloader.addEventListener(FlexEvent.INIT_COMPLETE, init_complete);

			resize(null);
		}

		private function remove():void
		{
			_preloader.removeEventListener(ProgressEvent.PROGRESS, load_progress);
			_preloader.removeEventListener(Event.COMPLETE, load_complete);
			_preloader.removeEventListener(FlexEvent.INIT_PROGRESS, init_progress);
			_preloader.removeEventListener(FlexEvent.INIT_COMPLETE, init_complete);
		}

		private function load_progress(e:ProgressEvent):void
		{
		}

		private function load_complete(e:Event):void
		{

		}

		private function init_progress(e:FlexEvent):void
		{

		}

		private function init_complete(e:FlexEvent):void
		{
			remove();

			var timer:Timer=new Timer(2000, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
			timer.start();
		}

		private function onTimerComplete(e:TimerEvent):void
		{
			//最后这个地方需要dpe一个Event.COMPLETE事件..表示加载完毕让swf继续操作~  
			dispatchEvent(new Event(Event.COMPLETE))
		}
	}
}